package com.hotel.BL;

import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.FetchOptions;

import data.Hotel;

public class HotelBuilderLogic {
	private static HotelBuilderLogic uniqueInstance;
	
	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	
	private HotelBuilderLogic(){}
	
	public static HotelBuilderLogic getInstance(){
		if(uniqueInstance == null){
			uniqueInstance = new HotelBuilderLogic();
		}
		return uniqueInstance;
	}
	
	public void CreateHotel(String hotelName)
	{
		Transaction txn = datastore.beginTransaction();
		try {
			long id=0;
			
			Query query = new Query(EntityName.Hotel);
			query.setKeysOnly();
			List<Entity> list = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
			id = list.size() + 1;

			Hotel hotel = new Hotel();
			hotel.setHotelName(hotelName);
			hotel.setId(id);
			datastore.put(hotel.getEntity());

		    txn.commit();
		} finally {
		    if (txn.isActive()) {
		        txn.rollback();
		    }
		}
	}
}
